//
// AWS Manager - A free AWS console application demonstrating advanced concepts
// Copyright (C) 2008-2010 Sean O'Dell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awsmanager.common;

import com.sun.data.provider.RowKey;
import com.sun.data.provider.impl.ObjectListDataProvider;
import com.xerox.amazonws.ec2.EC2Exception;
import com.xerox.amazonws.ec2.ImageListAttribute;
import com.xerox.amazonws.ec2.Jec2.ImageListAttributeOperationType;
import com.xerox.amazonws.ec2.LaunchPermissionAttribute;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sean ODell
 */
public class EC2AMILaunchPermissionDataProvider extends ObjectListDataProvider {

    private ArrayList ec2AMILPList = new ArrayList();

    public EC2AMILaunchPermissionDataProvider() {
        setList(ec2AMILPList);
        setObjectType(EC2AMILaunchPermission.class);
    }

    public EC2AMILaunchPermission getAMILaunchPermission(RowKey rowKey) {
        EC2AMILaunchPermission ec2AMILaunchPermission = (EC2AMILaunchPermission) getObject(rowKey);
        return ec2AMILaunchPermission;
    }

    public void load(Map ec2AMILPInfo,
            EC2ImageDataProvider ec2ImageDataProvider) {

        ArrayList rowKeysList = (ArrayList) ec2AMILPInfo.get("rowKeys");
        ArrayList<String> awsAccountIDs = null;

        clearObjectList();

        if (ec2ImageDataProvider == null) {
            return;
        }

        for (int i = 0; i < rowKeysList.size(); i++) {

            EC2Image ec2Image = ec2ImageDataProvider.getImage((RowKey) rowKeysList.get(i));
            EC2AMILaunchAttributes ec2AMILaunchAttributes =
                    ec2ImageDataProvider.getImageAttribute(ec2Image.getImageRegion(),
                    ec2Image.getimageId());

            if (ec2AMILaunchAttributes != null) {
                awsAccountIDs = ec2AMILaunchAttributes.getAwsAccountIDs();
                if (awsAccountIDs != null) {
                    for (String awsAccountID : awsAccountIDs) {
                        if (!awsAccountID.equals("all")) {
                            EC2AMILaunchPermission ec2AMILaunchPermission = new EC2AMILaunchPermission();
                            ec2AMILaunchPermission.setAmiRegion(ec2Image.getImageRegion());
                            ec2AMILaunchPermission.setAmiID(ec2Image.getimageId());
                            ec2AMILaunchPermission.setAmiLPAccountID(awsAccountID);
                            getList().add(ec2AMILaunchPermission);
                        }
                    }
                }
            }
        }
    }

    public String revokeLaunchPermission(
            EC2Connection ec2connection,
            RowKey[] selectedRowKeys) {

        String statusMsg = new String();

        statusMsg = statusMsg + "Revoked Launch Permissions: \n\n";

        for (int i = 0; i < selectedRowKeys.length; i++) {
            RowKey rowKey = selectedRowKeys[i];

            EC2AMILaunchPermission ec2AMILaunchPermission = getAMILaunchPermission(rowKey);
            EC2Region ec2RegionInfo = null;

            ec2RegionInfo = (EC2Region) ec2connection.getEc2RegionInfo().get(
                    ec2AMILaunchPermission.getAmiRegion());

            ImageListAttribute imageListAttribute = new LaunchPermissionAttribute();
            imageListAttribute.addImageListAttributeItem(
                    ImageListAttribute.ImageListAttributeItemType.userId,
                    ec2AMILaunchPermission.getAmiLPAccountID());
            try {
                ec2RegionInfo.getRegionConnection().modifyImageAttribute(
                        ec2AMILaunchPermission.getAmiID(),
                        imageListAttribute, ImageListAttributeOperationType.remove);
                statusMsg = statusMsg + "Launch Permission: " +
                        ec2AMILaunchPermission.getAmiRegion() +
                        ", " + ec2AMILaunchPermission.getAmiID() +
                        ", " + ec2AMILaunchPermission.getAmiLPAccountID() +
                        "\n";
            } catch (EC2Exception ex) {
                Logger.getLogger(EC2AMILaunchPermissionDataProvider.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return statusMsg;
    }
}
